package webapp.mvc.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponseWrapper;
import java.io.IOException;

public class HttpUtil {
    private static final Logger LOG = LoggerFactory.getLogger(HttpUtil.class);
    
    public static void safeWrite(HttpServletResponseWrapper response, CharSequence str){
        try {
            response.getWriter().append(str);
        } catch (IOException e) {
            LOG.error("io error", e);
        }
    }

    public static int getIntRequestParam(HttpServletRequest request, String key, int defaultValue) {
        String strValue = request.getParameter(key);
        try {
            return Integer.valueOf(strValue);
        } catch (Exception e) {
            return defaultValue;
        }
    }
}
